﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Apps.IBLL;
using Microsoft.Practices.Unity;
using Apps.IDAL;
using Apps.Models;
using Apps.Common;
using Apps.BLL.Core;
using Apps.Models.Sys;
using System.Transactions;
using Apps.IBLL.Sys;
using Apps.IDAL.Sys;

namespace Apps.BLL.Sys
{
    public class SysRightGetUserRightBLL :  ISysRightGetUserRightBLL
    {
        [Dependency]
        public ISysRightGetUserRightRepository sysRightGetUserRightRepository { get; set; }

        public GridRows<SysRightUserRight> GetList(string UserId, string isApi, int PageIndex, int PageSize)
        {
            using (DBContainer db = new DBContainer())
            {
                var mUser = db.SysUser.Where(m => m.Id == UserId).FirstOrDefault();

                List<SysRightUserRight> RightByUser_Results = new List<SysRightUserRight>();
                var AllModules = new List<SysModule>();

                foreach (var Role in mUser.SysRole)
                {
                    foreach (var Right in Role.SysRight.Where(m => m.Rightflag))
                    {
                        if ((isApi == "api") ? Right.SysModule.Name.Contains("000Api") : !Right.SysModule.Name.Contains("000Api") && Right.SysModule.IsLast && AllModules.Where(m => m.Id == Right.SysModule.Id).Count() == 0)
                        {
                            AllModules.Add(Right.SysModule);
                        }
                    }
                }
               
                var Modules = AllModules.OrderBy(m=>m.PATH).ThenBy(m=>m.Sort).Skip(PageIndex * PageSize).Take(PageSize).ToList();
                foreach (var mModule in Modules)
                {
                    if (RightByUser_Results.Where(m => m.ModuleId == mModule.Id).Count() == 0)
                    {
                        var SelOperates = new List<SysRightOperate>();

                        var mRightByUserResult = new SysRightUserRight();
                        mRightByUserResult.ModuleId = mModule.Id;

                        foreach (var SysRight in mModule.SysRight.Where(m=>mUser.SysRole.Contains(m.SysRole)))
                        {
                            foreach (var Operate in SysRight.SysRightOperate.Where(m=>m.IsValid))
                            {
                                if (SelOperates.Where(m => Operate.Id == m.Id).Count() == 0)
                                {
                                    SelOperates.Add(Operate);
                                }
                            }
                        }

                        var keyCode = "";
                        foreach (var ModuleOperat in mModule.SysModuleOperate.OrderBy(m => m.Sort))
                        {
                            if (SelOperates.Where(m => m.KeyCode == ModuleOperat.KeyCode).Count() > 0)
                            {
                                keyCode += "<a class=\"fa fa-check-square-o color-green\"/>" + ModuleOperat.Name;
                            }
                            else
                            {
                                keyCode += "<a class=\"fa fa-square-o color-red\"/>" + ModuleOperat.Name;
                            }
                        }
                        mRightByUserResult.KeyCode = keyCode;

                        var Model = mModule;
                        var ModelName = mModule.Name;
                        bool isHaveParent = mModule.SysModule2 != null;
                        while (isHaveParent)
                        {
                            Model = GetParentModel(Model);
                            if (Model != null)
                            {
                                ModelName = Model.Name + "<a class=\"fa fa-angle - double - right\" />>>" + ModelName;
                                isHaveParent = true;
                            }
                            else
                            {
                                isHaveParent = false;
                            }
                        }
                        mRightByUserResult.ModuleName = ModelName;
                        RightByUser_Results.Add(mRightByUserResult);
                    }
                }
                return new GridRows<SysRightUserRight>() { rows = RightByUser_Results, total = AllModules.Count() };
            }
        }

        private SysModule GetParentModel(SysModule Child)
        {
            return Child.SysModule2;
        }
    }
}
